package com.copm.ifm.servers.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.copm.ifm.base.api.pojo.PageDTO;
import com.copm.ifm.base.basic.pojo.vo.DictContentVO;
import com.copm.ifm.base.basic.util.AuthUtil;
import com.copm.ifm.servers.sys.entity.SysBusinessType;
import com.copm.ifm.servers.sys.entity.SysStaff;
import com.copm.ifm.servers.sys.entity.SysTenant;
import com.copm.ifm.servers.sys.mapper.SysBusinessTypeMapper;
import com.copm.ifm.servers.sys.mapper.SysStaffMapper;
import com.copm.ifm.servers.sys.mapper.SysTenantMapper;
import com.copm.ifm.servers.sys.service.SysBusinessTypeService;
import com.copm.ifm.servers.sys.service.SysDictService;
import com.copm.ifm.servers.sys.vo.SysBusinessTypeVO;
import com.copm.ifm.servers.sys.vo.SysDictVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 业务类型表 服务实现类
 *
 * @author tp
 * @since 2021-01-20
 */
@Service
public class SysBusinessTypeServiceImpl extends ServiceImpl<SysBusinessTypeMapper, SysBusinessType> implements SysBusinessTypeService {

    @Autowired
    SysDictService sysDictService;

    @Resource
    SysBusinessTypeMapper sysBusinessTypeMapper;

    @Resource
    SysTenantMapper sysTenantMapper;

    @Resource
    SysStaffMapper sysStaffMapper;

    @Override
    public List<SysBusinessTypeVO> selectForPage(PageDTO<SysBusinessType> param, Integer tenantId, Integer isDisplay) {
        List<SysBusinessTypeVO> businessTypeList = sysBusinessTypeMapper.selectListVO(param.getParam(), tenantId, isDisplay);
        SysTenant sysTenant = new SysTenant();
        sysTenant.setId(tenantId);
        sysTenant.setAllAuth(0);
        sysTenantMapper.updateById(sysTenant);
        List<SysBusinessTypeVO> sysBusinessTypeList = new ArrayList<>();
        if (businessTypeList == null || businessTypeList.size() == 0) {
            SysDictVO sysDictVO = sysDictService.selectByCode("BUSINESS_TYPE", 1);
            ArrayList<DictContentVO> content = sysDictVO.getContent();
            content.forEach(s -> {
                SysBusinessType sysBusinessType = new SysBusinessType();
                sysBusinessType.setCode(String.valueOf(s.getKey()));
                sysBusinessType.setName(s.getValue());
                sysBusinessType.setCreateBy(AuthUtil.getCurrentUserId());
                sysBusinessType.setUpdateBy(AuthUtil.getCurrentUserId());
                sysBusinessType.setTenantId(tenantId);
                sysBusinessType.setIsAuth(1);
                sysBusinessTypeMapper.insert(sysBusinessType);
                SysBusinessTypeVO sysBusinessTypeVO = new SysBusinessTypeVO();
                sysBusinessTypeVO.setIsAll(true);
                BeanUtils.copyProperties(sysBusinessType, sysBusinessTypeVO);
                SysStaff staff = sysStaffMapper.selectById(AuthUtil.getCurrentUserId());
                sysBusinessTypeVO.setUpdateByName(staff.getName());
                sysBusinessTypeList.add(sysBusinessTypeVO);
            });
            return pageVO(param, sysBusinessTypeList);
        }
        return pageVO(param, businessTypeList);

    }

    @Override
    public List<SysBusinessType> selectByTenantId(Integer tenantId) {
        return sysBusinessTypeMapper.selectList(new LambdaQueryWrapper<SysBusinessType>()
                .eq(SysBusinessType::getTenantId, tenantId));
    }

    /**
     * list集合物理分页方法
     */
    public List<SysBusinessTypeVO> pageVO(PageDTO<?> param, List<SysBusinessTypeVO> list) {
        List<SysBusinessTypeVO> resultTypeList;
        //list集合物理分页，特殊情况使用，不推荐（并发情况下有安全隐患）
        double i = (double) list.size() / (double) param.getPageSize();
        int ceil = (int) Math.ceil(i);
        if (param.getPageNum() == ceil) {
            resultTypeList = list.size() <= param.getPageSize() ? list
                    : list.subList((param.getPageNum() - 1) * param.getPageSize(), list.size());
        } else if (param.getPageNum() > ceil) {
            resultTypeList = null;
        } else {
            resultTypeList = list.size() <= param.getPageSize() ? list
                    : list.subList((param.getPageNum() - 1) * param.getPageSize(),
                    param.getPageNum() * param.getPageSize());
        }
        return resultTypeList;
    }

}
